package com.tanhua.management.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.system.UserInfo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.server.api.DashBoardApi;
import com.tanhua.management.enums.LogTypeEnum;
import com.tanhua.management.mapper.LogMapper;
import com.tanhua.management.mapper.UserInfoMapper;
import com.tanhua.management.mapper.UserMapper;
import com.tanhua.management.pojo.Log;
import com.tanhua.management.pojo.User;
import com.tanhua.management.vo.DateVo;
import com.tanhua.management.vo.InfoStatisticsVo;
import com.tanhua.management.vo.UsersStatisticsVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class DashBoardService {

    @Autowired
    private UserMapper userMapper;


    @Reference(version="1.0.0")
    private DashBoardApi dashBoardApi;


    /**
     * 统计
     *
     * @return
     */
    public InfoStatisticsVo InfoStatistics() {
        InfoStatisticsVo infoStatisticsVo = new InfoStatisticsVo();


        //1. 查询累计用户(查询mysql中的user表)
        Integer cumulativeUsersCount = this.userMapper.selectCount(null);
        infoStatisticsVo.setCumulativeUsers(cumulativeUsersCount);

        //新增用户（今日）
        QueryWrapper<User> todayUserQuery = new QueryWrapper<>();
        todayUserQuery.between("created", this.getUserTime(1), this.getUserTime(0));
        Integer todayRegister = this.userMapper.selectCount(todayUserQuery);
        infoStatisticsVo.setNewUsersToday(todayRegister);

        //今日环比增长率
        //新增用户（昨日）
        QueryWrapper<User> yesterdayUserQuery = new QueryWrapper<>();
        yesterdayUserQuery.between("created", this.getUserTime(2), this.getUserTime(1));
        Integer yesterdayRegister = this.userMapper.selectCount(todayUserQuery);

        infoStatisticsVo.setNewUsersTodayRate(8);


        //今日活跃用户
        Integer activeUsersToday = this.dashBoardApi.queryActiveUser(0);
        infoStatisticsVo.setActiveUsersToday(activeUsersToday);
        infoStatisticsVo.setActiveUsersTodayRate(-10);


        //昨日活跃用户
        Integer activeUserYesterday = this.dashBoardApi.queryLoginCount(1);
        infoStatisticsVo.setActiveUsersYesterday(activeUserYesterday);
        infoStatisticsVo.setActiveUsersYesterday(10);



        /*log表*/
        //过去7天活跃用户
/*        QueryWrapper<Log> tenDayQuery = new QueryWrapper<>();
        tenDayQuery.between("created", this.getPastDayEndTime(7), this.getLongTime(1));
        tenDayQuery.eq("method", "01");
        Integer activePassWeekCount = this.logMapper.selectCount(tenDayQuery);
        infoStatisticsVo.setActivePassWeek(activePassWeekCount);*/
        Integer activePassWeekActiveCount = this.dashBoardApi.queryActiveUser(7);
        infoStatisticsVo.setActivePassWeek(activePassWeekActiveCount);


        //过去30天活跃用户
/*        QueryWrapper<Log> monthQuery = new QueryWrapper<>();
        monthQuery.between("created", this.getPastDayEndTime(30), this.getLongTime(1));
        monthQuery.eq("method", LogTypeEnum.LOGIN.getValue());
        Integer activePassMonthCount = this.logMapper.selectCount(monthQuery);
        infoStatisticsVo.setActivePassMonth(activePassMonthCount);*/
        Integer activePassMonthActiveCount = this.dashBoardApi.queryActiveUser(30);
        infoStatisticsVo.setActivePassMonth(activePassMonthActiveCount);

        //今日登录数
        Integer loginCount = this.dashBoardApi.queryLoginCount(1);
        infoStatisticsVo.setLoginTimesTodayRate(loginCount);



        //过去7天平均日使用时长
        //todo








        //登录次数（今日）
        QueryWrapper<Log> loginCountQuery = new QueryWrapper<>();
        loginCountQuery.between("created", this.getLongTime(1), this.getLongTime(0));
        loginCountQuery.eq("method", LogTypeEnum.LOGIN.getValue());


        //活跃用户（今日）


        //填充
        //infoStatisticsVo.setCumulativeUsers(integerCount);


        return null;
    }





    /**
     *
     * @param sd
     * @param ed
     * @param type
     * @return
     */
    public UsersStatisticsVo getUsers(Long sd, Long ed, Integer type) {

        //创建对象
        UsersStatisticsVo analysisUsersVo = new UsersStatisticsVo();
        List<DateVo> thisYear = new ArrayList<>();
        List<DateVo> lastYear = new ArrayList<>();

        //获取起始年月份
        /**
         * 设置去年的起始时间
         */
        Calendar sdate = Calendar.getInstance();
        sdate.setTime(new Date(sd));
        sdate.add(Calendar.YEAR, -1);
        Date sdTime = sdate.getTime();

        Calendar edate = Calendar.getInstance();
        edate.setTime(new Date(ed));
        edate.add(Calendar.YEAR, -1);
        Date edTime = edate.getTime();

        Date time = sdate.getTime();
        String month = new DateTime(time).toString("MM") + "月";

        Calendar thisSdDate = Calendar.getInstance();
        sdate.setTime(new Date(sd));
        sdate.add(Calendar.YEAR, -1);
        Date thisSdTime = sdate.getTime();

        Calendar thisEdDate = Calendar.getInstance();
        edate.setTime(new Date(ed));
        edate.add(Calendar.YEAR, -1);
        Date thisEdTime = edate.getTime();


        if (type == 101){
            //查询新增用户
            //lastYear
            QueryWrapper<User> duringQuery = new QueryWrapper<>();
            duringQuery.between("created", sdTime, edTime);
            Integer duringCount = this.userMapper.selectCount(duringQuery);

            //thisYear
            QueryWrapper<User> duringThisQuery = new QueryWrapper<>();
            duringQuery.between("created", thisSdDate, thisEdDate);
            Integer duringThisCount = this.userMapper.selectCount(duringThisQuery);


            //thisYear
            DateVo thisDateVo = new DateVo();
            thisDateVo.setTitle(month);
            thisDateVo.setAmount(duringThisCount);

            //lastYear
            DateVo lastDateVo = new DateVo();
            lastDateVo.setTitle(month);
            lastDateVo.setAmount(duringCount);


            thisYear.add(thisDateVo);
            thisYear.add(lastDateVo);

            analysisUsersVo.setThisYear(thisYear);
            analysisUsersVo.setLastYear(lastYear);
            return analysisUsersVo;
        }


        //活跃用户
        if (type == 102){
            Integer thisActiveUser = this.dashBoardApi.queryActiveUser(thisSdTime,thisEdTime);
            Integer lastActiveUser = this.dashBoardApi.queryActiveUser(sdTime,edTime);
            DateVo thisActiveCount = DateVo.builder().title(month).amount(thisActiveUser).build();
            DateVo lastActiveCount = DateVo.builder().title(month).amount(lastActiveUser).build();
            thisYear.add(thisActiveCount);
            thisYear.add(lastActiveCount);
            analysisUsersVo.setThisYear(thisYear);
            analysisUsersVo.setLastYear(lastYear);
        }

        if (type == 103){

        }
        return analysisUsersVo;
    }








    //获取昨天结束时间（今天开始）
    private Long getLongTime(int day) {
        Calendar yesterdayEnd = Calendar.getInstance();
        yesterdayEnd.set(Calendar.HOUR_OF_DAY, 0);
        yesterdayEnd.set(Calendar.MINUTE, 0);
        yesterdayEnd.set(Calendar.SECOND, 0);
        yesterdayEnd.add(Calendar.DATE, -day);
        return yesterdayEnd.getTime().getTime();
    }


    //获取过去七天的开始时间
    private Long getPastDayEndTime(int day) {
        Calendar yesterdayEnd = Calendar.getInstance();
        yesterdayEnd.set(Calendar.HOUR_OF_DAY, 0);
        yesterdayEnd.set(Calendar.MINUTE, 0);
        yesterdayEnd.set(Calendar.SECOND, 0);
        yesterdayEnd.add(Calendar.DATE, -(1 + day));
        return yesterdayEnd.getTime().getTime();
    }

    /*mysql*/
    //获取今天结束时间
    private Date getUserTime(int day) {
        Calendar yesterdayEnd = Calendar.getInstance();
        yesterdayEnd.set(Calendar.HOUR_OF_DAY, 0);
        yesterdayEnd.set(Calendar.MINUTE, 0);
        yesterdayEnd.set(Calendar.SECOND, 0);
        yesterdayEnd.add(Calendar.DATE, -day);
        return yesterdayEnd.getTime();
    }
}
